package com.bazinga.contact.controller;


import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.bazinga.contact.model.StudentSchedule;
import com.bazinga.contact.util.HibernateUtil;


public class StudentScheduleManager extends HibernateUtil {

	public StudentSchedule add(StudentSchedule studentSchedule) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(studentSchedule);
		session.getTransaction().commit();
		return studentSchedule;
	}
	public StudentSchedule delete(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		StudentSchedule studentSchedule = (StudentSchedule) session.load(StudentSchedule.class, id);
		if(null != studentSchedule) {
			session.delete(studentSchedule);
		}
		session.getTransaction().commit();
		return studentSchedule;
	}

	public List<StudentSchedule> list() {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<StudentSchedule> studentSchedule = null;
		try {
			
			studentSchedule = (List<StudentSchedule>)session.createQuery("from StudentSchedule").list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return studentSchedule;
	}
}
